using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BlogSystem.Ability.Docking.UserApp;
using BlogSystem.Ability.Docking.UserApp.Dto;
using BlogSystem.Domain.AccountInfo;
using Youshow.Ace.Ability;
using Youshow.Ace.Domain.Repository;

namespace BlogSystem.Ability.UserApp
{
    public class UserService : AbilityServicer, IUserService
    {
        public IRepository<User> UserRep{get; set;}

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task<LoginSuccessDto> CheckLogin(string userName, string password)
        {
            // var user = new User
            // {
            //     UserName = userName,
            //     Password = password
            // };
            //  user.checkLogin(UserRep);
            var user = await User.CheckLogin(userName, password, UserRep);
            var res =  ModelMapper.Map<User, LoginSuccessDto>(user);
            res.Token = Guid.NewGuid().ToString();
            return res;
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task<bool> RegisUser(CreateUserDto createUser)
        {
            try
            {
                var user = ModelMapper.Map<CreateUserDto, User>(createUser);
                user.ValidateRegist();
                await UserRep.InsertAsync(user);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}